<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use think\Db;

/**
 * 会员支付管理
 * Class AdminUser
 * @package app\admin\controller
 */
class MemberPay extends AdminBase
{
    //定义数据表
    private $db;

    // 构造函数 实例化Red表
    public function __construct() {
        parent::__construct();
        $this->db = Db::name('member_pay_log');
    }
    
    /**
     * 会员支付管理
     * @return mixed
     */
    public function index(){
        $id = input('param.member_id');
        
        //获取列表
        $data = $this->db->where(['member_id'=>$id])->order('id desc')->paginate(10);
        
        // 获取分页显示
        $page = $data->render();
        
        // 模板变量赋值
        $this->assign(array(
            'data' => $data,
            'page'=> $page
        ));
        
        return $this->fetch();
    }
    
    //会员支付添加
    public function add () {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            
            $result = $this->validate($data,'MemberPayLog.add');

            if(true  !== $result){
                $this->error($result);
            }else{
                $data['pay_time'] = strtotime($data['pay_time']);
                $data['expire_time'] = strtotime($data['expire_time']);
                
                $this->db->insert($data);
                
                if($data['expire_time'] > time()){  //如果设置的过期时间大于当前时间 那么设置会员的状态为已支付并且活跃
                    Db::name('member')->where(['id'=>$data['member_id']])->update(['pay_status'=>1,'level_status'=>1]);
                }
                
                $this->success('添加成功',url('member_pay/index',['member_id'=>$data['member_id']]));
            }	
        }
        
       return $this->fetch('',['member_level'=>cache('member_level')]);
    }
    
    //会员支付编辑
    public function edit () {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = $this->validate($data,'MemberPayLog.edit');

            if(true !== $result){
                $this->error($result);
            }else{
                $data['pay_time'] = strtotime($data['pay_time']);
                $data['expire_time'] = strtotime($data['expire_time']);
                
                $this->db->update($data,['id'=>$data['id']]);
                
                if($data['expire_time'] > time()){  //如果设置的过期时间大于当前时间 那么设置会员的状态为已支付并且活跃
                    Db::name('member')->where(['id'=>$data['member_id']])->update(['pay_status'=>1,'level_status'=>1]);
                }
                $this->success('修改成功');
            }	
        }
        
        $id = input('param.id');
        $data = $this->db->find($id);
        
       return $this->fetch('',['data'=>$data,'member_level'=>cache('member_level')]);
    }
    
    
    
   /**
     * 删除会员支付
     * @param int   $id
     * @param array $ids
     */
    public function delete($id = 0, $ids = [])
    {
        $id = $ids ? $ids : $id;
        if ($id) {
            if ($this->db->delete($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的记录');
        }
    }
}

    